const utils = require("../utils/utils.js")
var app = getApp()
Page({
  data: {
    path: "",
  },
  onLoad: function (options) {
    var imageUrl = options.imageUrl;
    var id = options.id
    console.log("=====" + imageUrl)
    this.setData({
      path: imageUrl,
      id: id
    })
    var token = tt.getStorageSync("TOKEN")
    if (token == undefined || token == "" || token == null) {
      app.GetBaiduAccessToken(function (res) {
        this.data.token = token
      })
    } else {
      this.data.token = token
    }
  },
  onTapOcr: function () {
    var that = this
    tt.getFileSystemManager().readFile({
      filePath: this.data.path,
      encoding: "base64",
      success: res => {
        that.reqNet(res.data)
      },
      fail: res => {
        wx.hideLoading()
      }
    })

  },
  getUrl: function () {
    var host = utils.url.host_url
    var url = ""
    switch (this.data.id) {
      case 11:
        return host + utils.url.general_basic;
      case 12:
        return host + utils.url.accurate_basic;
      case 21:// 省份证识别
        return host + utils.url.idcard;
      case 22:// 银行卡
        return host + utils.url.bankcard;
      case 23:// 营业执照
        return host + utils.url.business_license;
      case 24://护照
        return host + utils.url.passport;
      case 25://港澳通行证
        return host + utils.url.HK_Macau_exitentrypermit;
      case 26://台湾通行证
        return host + utils.url.taiwan_exitentrypermit;
      case 27://户口本
        return host + utils.url.household_register;
      case 28://名片
        return host + utils.url.business_card;
      case 29://出生医学证明
        return host + utils.url.birth_certificate;
      case 31://增值税发票
        return host + utils.url.accurate_basic;
      case 32://定额发票
        return host + utils.url.accurate_basic;
      case 33://通用机打发票
        return host + utils.url.accurate_basic;
      case 34://火车票
        return host + utils.url.accurate_basic;
      case 35://出租车票
        return host + utils.url.accurate_basic;
      case 36://行程单
        return host + utils.url.accurate_basic;
      case 37://彩票
        return host + utils.url.accurate_basic;
      case 38://保险单
        return host + utils.url.accurate_basic;
      case 39://通用票据
        return host + utils.url.accurate_basic;
      case 41://车牌
        return host + utils.url.accurate_basic;
      case 42://VIN 码
        return host + utils.url.accurate_basic;
      case 43://驾驶证
        return host + utils.url.accurate_basic;
      case 44://行驶证
        return host + utils.url.accurate_basic;
      case 45://机动车销售
        return host + utils.url.accurate_basic;
      case 46://车辆合格证
        return host + utils.url.accurate_basic;
      case 51://表格
        return host + utils.url.accurate_basic;
      case 52://手写文字
        return host + utils.url.accurate_basic;
      case 53://网络图片
        return host + utils.url.accurate_basic;
      case 54://数字
        return host + utils.url.accurate_basic;
      case 55://二维码识别
        return host + utils.url.accurate_basic;
        default :
        return host + utils.url.general_basic;
    }

  },
  reqNet: function (data) {
    var that = this
    var url=that.getUrl()
    app.func.reqLoading(url+"?access_token="+ this.data.token,
      {
        image: data,
        probability: true
      }, function (res) {
        console.log("baidu Ai===" + JSON.stringify(res))
        var result = JSON.stringify(res.words_result)
        if (result == null || result == "") {
          tt.showToast({
            title: '未识别到文字', // 内容
            icon: 'none'
          });
        } else {
          console.log("===="+'url=' + that.data.path + '&content=' + encodeURIComponent(result))
          tt.navigateTo({
            url: '/pages/result/result?url=' + that.data.path + '&content=' + encodeURIComponent(result) // 指定页面的url
          });
        }

      }, "POST")
  }
})